package com.ball;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.WindowManager;

public class ColorBall {
	private Bitmap myImage;
	private double myX, myY, myId, myXV, myYV, myRadius;
	public static int BallCount=1;
	private static double Y_MAX=400, X_MAX=270;
	//private double myDirection, mySpeed;
	public ColorBall(Context context, View v, double x, double y, double radius) {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		myId = BallCount;
		BallCount++;
		//WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		//Y_MAX=manager.getDefaultDisplay().getHeight();
		//X_MAX=manager.getDefaultDisplay().getWidth();
		Y_MAX=v.getHeight();
		X_MAX=v.getWidth();
		myX=x;
		myY=y;
		myXV=5;
		myYV=5;
		myRadius=radius;
	}
	public void onMove(int x, int y, ColorBall Ball2) {
		myX+=myXV;
		myY+=myYV;
		double excessPartOfMove=(double).5;
		if (myY>Y_MAX-myRadius) {
			excessPartOfMove=(myY-Y_MAX+myRadius)/(myYV);
			myY=Y_MAX-myRadius;
			myX-=excessPartOfMove*myXV;
			myYV*=-1;
		} else if (myY<myRadius) {
			excessPartOfMove=(myRadius-myY)/(myYV);
			myY=myRadius+(double).01;
			myX-=excessPartOfMove*myXV;
			myYV*=-1;
		}
		if (myX>X_MAX-myRadius) {
			excessPartOfMove=(myX-X_MAX+myRadius)/(myXV);
			myX=X_MAX-myRadius;
			myY-=excessPartOfMove*myYV;
			myXV*=-1;
		} else if (myX<myRadius-(double).01) {
			excessPartOfMove=(myRadius-myX)/(myXV);
			myX=myRadius;
			myY-=excessPartOfMove*myYV;
			myXV*=-1;
		}
		double magnitude, dist;
		dist=distance(this.getX(), this.getY(), Ball2.getX(), Ball2.getY());
		magnitude=30.0/(Math.pow(dist, -2.0)+this.getRadius()+Ball2.getRadius());
		this.setXV(this.getXV()+(this.getX()-Ball2.getX())*magnitude/dist);
		this.setYV(this.getYV()+(this.getY()-Ball2.getY())*magnitude/dist);
		Ball2.setXV(Ball2.getXV()+(Ball2.getX()-this.getX())*magnitude/dist);
		Ball2.setYV(Ball2.getYV()+(Ball2.getY()-this.getY())*magnitude/dist);
	}
	private double distance(double x1, double y1, double x2, double y2) {
		double c2 = (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
		return Math.pow(c2, 1.0/2.0);
	}
	
	
	
	public void setX(double X) {myX=X;}
	public double getX() {return myX;}
	public void setY(double Y) {myY=Y;}
	public double getY() {return myY;}
	public void setXV(double XV) {myXV=XV;}
	public double getXV() {return myXV;}
	public void setYV(double YV) {myYV=YV;}
	public double getYV() {return myYV;}
	public double getId() {return myId;}
	public double getRadius() {return myRadius;}
	public Bitmap getBitmap() {return myImage;}
}
